import { ElLoading } from 'element-plus'

interface ILoadingInstance {
	_target?: any // 没找到 ElLoading.service 实例声明 any 代替
	_count: number
}

class LoadingClass {
	private loadingInstance: ILoadingInstance = {
		_target: null,
		_count: 0,
	}

	loadingOptions = {
		lock: true,
		text: '加载中...',
		// background: 'rgba(255,255,255,0)'
	}

	add_count() {
		this.loadingInstance._count++
	}

	set_loadingInstance_target(loading: boolean) {
		if (loading) {
			this.add_count()
			this.loadingInstance._count === 1 && (this.loadingInstance._target = ElLoading.service(this.loadingOptions))
		}
	}

	remove_loadingInstance_target(loading: boolean) {
		if (loading && this.loadingInstance._count > 0) {
			this.loadingInstance._count--
		}
		if (loading && this.loadingInstance._count === 0) {
			this.loadingInstance._target?.close && this.loadingInstance._target.close()
			this.loadingInstance._target = null
		}
	}
}

export default new LoadingClass()
